﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Xml;
using System.IO;
using MySql.Data.MySqlClient;
using Models;

namespace DAL
{
    public class LinkService
    {
        public bool isLink(string ser,string db,string id,string pwd,string cha, string code)
        {
            XmlWriter writer = null;
            XmlWriterSettings xws = new XmlWriterSettings();//1
            xws.Indent = true;//2
            xws.IndentChars = "\t";//3
            //1 2 3 xml格式化输出
            try
            {
                writer = XmlWriter.Create("config.xml", xws);
                writer.WriteStartElement("conn");
                writer.WriteElementString("server", ser);
                writer.WriteElementString("database", db);
                writer.WriteElementString("uid", id);
                writer.WriteElementString("pwd", pwd);
                writer.WriteElementString("Charset", cha);
                writer.WriteElementString("code", code);
                writer.WriteEndDocument();
                writer.Flush();
            }
            finally
            {
                if (writer != null)
                    writer.Close();
            }

            MySqlConnection conn = new MySqlConnection(DBHelper.getconnstr());
            try
            {
                conn.Open();
                return true;
            }
            catch
            {
                File.Delete("config.xml");
            }
            finally
            {
                conn.Close();
            }
            return false;
        }
    }
}
